package com.artbo.sporttema.pages;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.IncludeStylesheet;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.beaneditor.Validate;
import org.apache.tapestry5.corelib.components.Form;
import org.hibernate.Session;

import com.artbo.sporttema.entities.User;

public class Register {

	@Component
	private Form registerForm;

	@Property
	@Validate("required, minlength=3, maxlength=50")
	private String login;

	@Property
	@Validate("required, minlength=3, maxlength=50")
	private String password;

	@Property
	@Validate("required, minlength=3, maxlength=50")
	private String verifyPassword;

	@Property
	@Validate("email")
	private String email;
	
	@Property
	@Validate("required, minlength=3, maxlength=50")
	private String name;
	
	@Property
	@Validate("required, minlength=3, maxlength=50")
	private String surname;
	
	@Property
	private String patronymic;
	
	@Property
	@Validate("required, minlength=3, maxlength=50")
	private String sport;
	
    @SuppressWarnings("unused")
    @Property
    private String kaptcha;

	@InjectPage
	private Index index;

	@Inject
	private Session session;

	@OnEvent(value = EventConstants.VALIDATE, component = "RegisterForm")
	public void checkForm() {
		if (!verifyPassword.equals(password)) {
			registerForm.recordError("Пароли не совпадают");
		}
	}

	@OnEvent(value = EventConstants.SUCCESS, component = "RegisterForm")
	public Object proceedSignup() {
		User user = new User(login, password, email, name,
				surname, patronymic, sport);
		session.saveOrUpdate(user);
		return index;
	}

}